Imports System.ComponentModel

<Description("Singelton trida. Singelton instance obsahuje nastaveni pro komunikaci s COM rozhranim Office.")> _
Public NotInheritable Class WrapperSettings
    Private Shared theOne As WrapperSettings

	<Description("Singelton instance. Jedina mozna instance teto tridy.")> _
	Public Shared ReadOnly Property [Default]() As WrapperSettings
		Get
			If theOne Is Nothing Then theOne = New WrapperSettings
			Return theOne
		End Get
	End Property

	Private m_CollectionsIndexCorrection As Integer
	Private m_ReturnZeroBasedArrays As Boolean
	Private m_CloseApplicationOnDispose As Boolean

    <Description("Korekce indexu pri pristupu do kolekci. Nastavena hodnota se odecita od indexu zadanym uzivatelem (vyvojarem)." + _
        "Tzn. pokud chcete kolekce indexovat od 0, nastavte tuto hodnotu na 1. Vychozi nastaveni je 0.")> _
    Public Property CollectionsIndexCorrection() As Integer
        Get
            Return Me.m_CollectionsIndexCorrection
        End Get
        Set(ByVal Value As Integer)
            Me.m_CollectionsIndexCorrection = Value
            Me.OnCollectionsIndexCorrectionChanged(EventArgs.Empty)
        End Set
    End Property

    <Description("Pokud true, hodnoty typu Array (a jeji potomci) budou pri navraceni pretransformovani na pole indexovane od 0. Vychozi nastaveni je False.")> _
    Public Property ReturnZeroBasedArrays() As Boolean
        Get
            Return Me.m_ReturnZeroBasedArrays
        End Get
        Set(ByVal Value As Boolean)
            Me.m_ReturnZeroBasedArrays = Value
            Me.OnReturnZeroBasedArraysChanged(EventArgs.Empty)
        End Set
    End Property

    <Description("Pokud true, tak pri volani Dispose ci destruktoru objektu applikace se zavola Quit() na obalovanem objektu. Vychozi nastaveni je False.")> _
    Public Property CloseApplicationOnDispose() As Boolean
        Get
            Return Me.m_CloseApplicationOnDispose
        End Get
        Set(ByVal Value As Boolean)
			Me.m_CloseApplicationOnDispose = Value
            Me.OnCloseApplicationOnDisposeChanged(EventArgs.Empty)
        End Set
    End Property

	<Description("Pretransformuje pole na pole indexovane od 0.")> _
	Public Shared Function CorrectArrayToZeroBased(ByVal arr As Array) As Array
        If arr Is Nothing Then Throw New ArgumentNullException("arr")
		Dim dimensions As Integer = arr.Rank
		Dim lengths(dimensions - 1) As Integer
		Dim bases(dimensions - 1) As Integer
		Dim lowerBounds(dimensions - 1) As Integer

		For i As Integer = 0 To dimensions - 1
			lengths(i) = arr.GetLength(i)
			bases(i) = arr.GetLowerBound(i)
			lowerBounds(i) = 0
		Next

		Dim ret As Array = Array.CreateInstance(GetType(Object), lengths, lowerBounds)

		Dim iCorrection As Integer = 0
		For i As Integer = 0 To dimensions - 1
			Dim len As Integer = lengths(i)
			Dim base As Integer = bases(i)

			For j As Integer = 0 To len - 1
LAST_LOOP_START:
				Dim iindex As Integer = i + iCorrection
				Dim jindex As Integer = j + base
				Dim obj As Object
				Try
					obj = arr(jindex, iindex) 'arr(iindex, jindex)
                Catch exc As IndexOutOfRangeException
                    iCorrection += 1
                    GoTo LAST_LOOP_START
				End Try

				ret(j, i) = obj
			Next
		Next

		Return ret
	End Function

    <Description("Upravi index od uzivatele (vyvojare) na index pro nativni (COM+) kolekci.")> _
    Public Sub CorrectCollectionIndex(ByRef index As Integer)
        index = index + Me.CollectionsIndexCorrection
    End Sub

    <Description("Spousti udalost CollectionsIndexCorrectionChanged.")> _
    Private Sub OnCollectionsIndexCorrectionChanged(ByVal e As EventArgs)
        RaiseEvent CollectionsIndexCorrectionChanged(Me, e)
    End Sub

    <Description("Spousti udalost ReturnZeroBasedArraysChanged.")> _
    Private Sub OnReturnZeroBasedArraysChanged(ByVal e As EventArgs)
        RaiseEvent ReturnZeroBasedArraysChanged(Me, e)
    End Sub

    <Description("Spousti udalost CloseApplicationOnDisposeChanged.")> _
    Private Sub OnCloseApplicationOnDisposeChanged(ByVal e As EventArgs)
        RaiseEvent CloseApplicationOnDisposeChanged(Me, e)
    End Sub

    <Description("Upozornuje, ze se hodnota vlastnosti CollectionsIndexCorrection zmenila.")> _
    Public Event CollectionsIndexCorrectionChanged As EventHandler
    <Description("Upozornuje, ze se hodnota vlastnosti ReturnZeroBasedArrays zmenila.")> _
    Public Event ReturnZeroBasedArraysChanged As EventHandler
    <Description("Upozornuje, ze se hodnota vlastnosti CloseApplicationOnDispose zmenila.")> _
    Public Event CloseApplicationOnDisposeChanged As EventHandler
End Class
